package com.tinyengine.it.controller.endpoint;

import cn.hutool.core.bean.BeanUtil;
import com.tinyengine.it.common.base.Result;
import com.tinyengine.it.model.entity.ApiDirectory;
import com.tinyengine.it.model.vo.ApiDirectoryVo;
import com.tinyengine.it.service.endpoint.ApiDirectoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/interface-definition/api")
@Tag(name = "接口目录")
@RequiredArgsConstructor
public class ApiDirectoryController {

    private final ApiDirectoryService apiDirectoryService;

    @Operation(summary = "获取目录TREE",
            parameters = {
                    //@Parameter(name = "searchKey", description = "搜索关键字")
            },
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = ApiDirectory.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    //@SystemControllerLog(description = "获取项目、服务、分组TREE")
    @GetMapping("/getProjectServiceGroupInfo")
    public Result<List<ApiDirectoryVo>> getProjectServiceGroupInfo(@RequestHeader(required = false) Integer appId) {
        List<ApiDirectoryVo> projects = apiDirectoryService.getAllDirectory(appId);
        return Result.success(projects);
    }

    @Operation(summary = "新增及更新",
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = ApiDirectory.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    @PostMapping("/addAndUpdate")
    public Result addAndUpdate(@RequestBody @Valid ApiDirectoryVo apiProjects, @RequestHeader(required = false) Integer appId) {
        ApiDirectory apiDirectory = BeanUtil.copyProperties(apiProjects, ApiDirectory.class);
        apiDirectory.setAppId(appId);
        apiDirectoryService.saveOrUpdate(apiDirectory);
        return Result.success(apiDirectory.getId());
    }


    @Operation(summary = "根据ID删除",
            parameters = {
                    @Parameter(name = "id", description = "ID")},
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = Result.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    @GetMapping("/deleteById/{id}")
    public Result deleteProjectServiceGroupInf1o(@Valid @PathVariable Integer id) {
        apiDirectoryService.removeById(id);
        return Result.success();
    }
}
